package com.ylw.project.handler.impl;

import com.ylw.project.compile.ProjectSystem;
import com.ylw.project.handler.Handler;
import com.ylw.project.handler.chain.HandlerChain;
import com.ylw.project.handler.chain.impl.HandlerChainImpl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.log4j.Logger;

/**
 * @Description
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/11/10
 */
public class DirectoryHandler implements Handler {
    private static final Logger logger = Logger.getLogger(DirectoryHandler.class);

    @Override
    public Object handler(HandlerChain chain, String filePath) {

        if(isDirectory(filePath)){
            File dir = new File(filePath);
            if(!dir.exists()){
                return null;
            }
            String[] subFiles = dir.list();
            ThreadPoolExecutor pool = ProjectSystem.getPool();
            logger.info("start time " + new Date());
            for (int i = 0; i < subFiles.length; i++) {
                pool.execute(new HandlerChainImpl(dir.getAbsolutePath()+File.separator+subFiles[i]));
            }
        }else{
           chain.doHandler(chain.nextHandler());
        }
        return null;
    }

    /**
     * 检查该文件是否为java文件
     *
     * @param filePath
     * @return
     */
    private boolean isDirectory(String filePath) {
        File dir = new File(filePath);
        return dir.isDirectory();
    }


    public static void main(String[] args) {
        DirectoryHandler handler = new DirectoryHandler();
        handler.handler(null,"/Users/away/work/ideaProject/IW/src/main/java/com/ylw/project/handler/impl");
    }


}
